package com.wq.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.Result;
import com.wq.dto.ApplicationDTO;
import com.wq.dto.ApprovalDTO;
import com.wq.entity.Application;
import com.wq.service.ApplicationService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wq
 * @since 2025-11-09
 */
@RestController
@RequestMapping("/application")
public class ApplicationController {

    @Resource
    private ApplicationService applicationService;

    /**
     * 提交请假申请
     */
    @PostMapping("/submit")
    public Result<Application> submitApplication(@RequestBody ApplicationDTO applicationDTO) {
        Application application = applicationService.submitApplication(applicationDTO);
        return Result.success(application);
    }

    /**
     * 查询我的请假申请
     */
    @GetMapping("/my")
    public Result<Page<Application>> getMyApplications(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Application> page = applicationService.getMyApplications(pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 查询待审批列表（部门经理/系统管理员）
     */
    @GetMapping("/pending")
    public Result<Page<Application>> getPendingApprovals(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Application> page = applicationService.getPendingApprovals(pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 审批请假申请
     */
    @PostMapping("/approve")
    public Result<Boolean> approveApplication(@RequestBody ApprovalDTO approvalDTO) {
        Boolean result = applicationService.approveApplication(approvalDTO);
        return Result.success(result);
    }

    /**
     * 取消请假申请
     */
    @PutMapping("/cancel/{id}")
    public Result<Boolean> cancelApplication(@PathVariable Integer id) {
        Boolean result = applicationService.cancelApplication(id);
        return Result.success(result);
    }

    /**
     * 查询所有请假申请（系统管理员）
     */
    @GetMapping("/all")
    public Result<Page<Application>> getAllApplications(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Application> page = applicationService.getAllApplications(pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 查询审批历史（部门经理/系统管理员）
     */
    @GetMapping("/history")
    public Result<Page<Application>> getApprovalHistory(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Application> page = applicationService.getApprovalHistory(pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 根据ID查询详情
     */
    @GetMapping("/{id}")
    public Result<Application> getById(@PathVariable Integer id) {
        Application application = applicationService.getById(id);
        return Result.success(application);
    }
}
